package com.maoyan.quickdevelop.controller.dqsc;

import cn.dev33.satoken.annotation.SaCheckLogin;
import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.dev33.satoken.stp.StpUtil;
import com.github.pagehelper.PageInfo;
import com.maoyan.quickdevelop.common.annotation.Log;
import com.maoyan.quickdevelop.common.constant.HttpStatus;
import com.maoyan.quickdevelop.common.core.AjaxResult;
import com.maoyan.quickdevelop.common.core.domain.DqArticle;
import com.maoyan.quickdevelop.common.core.domain.DqSc;
import com.maoyan.quickdevelop.common.enums.BusinessType;
import com.maoyan.quickdevelop.common.utils.DateUtils;
import com.maoyan.quickdevelop.common.utils.StringUtils;
import com.maoyan.quickdevelop.system.service.IDqArticleService;
import com.maoyan.quickdevelop.system.service.IDqScService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * @author 猫颜
 * @date 2021/5/29 8:49
 * TODO 文章管理类
 */
@RestController
@RequestMapping("/dqsc")
public class DqScController {
    @Autowired
    private IDqScService iScService;
    @Autowired
    private IDqArticleService iDqArticleService;

    @GetMapping("/author/{authorId}")
    @ApiOperation(value = "根据作者的ID查询文章")
    public AjaxResult getByAuthorId(@RequestParam(defaultValue = "1") int pageNum,
                                    @RequestParam(defaultValue = "10") int pageSize,
                                    @PathVariable Long authorId) {
        DqSc dqSc = new DqSc();
        dqSc.setScUser(authorId);
        List<DqSc> dqScs = iScService.selectDqSc(pageNum, pageSize, dqSc);
        if (StringUtils.isNull(dqScs) || dqScs.size() == 0) {
            return AjaxResult.error(HttpStatus.NOT_FOUND, "此用户没有收藏");
        }
        List<DqArticle> articles = new ArrayList<>();
        for (DqSc sc : dqScs) {
            DqArticle dqArticle = iDqArticleService.selectDqArticleById(sc.getScArticle());
            articles.add(dqArticle);
        }
        return AjaxResult.success("查询成功", new PageInfo<>(articles));
    }

    @RequestMapping("/delete/{articleId}")
    @ApiOperation(value = "根据ID删除收藏")
    public AjaxResult deleteByScId(@PathVariable Long articleId) {
        DqSc dqSc = new DqSc();
        dqSc.setScArticle(articleId);
        dqSc.setScUser(StpUtil.getLoginIdAsLong());
        List<DqSc> dqScs = iScService.selectDqSc1(1, 5, dqSc);
        for (DqSc sc : dqScs) {
            iScService.deleteDqScById(sc.getScId());
        }
        return AjaxResult.success("删除成功", 1);
    }

    @RequestMapping("/add/{articleId}")
    @Log(title = "收藏操作", businessType = BusinessType.INSERT)
    @ApiOperation(value = "收藏文章")
    public AjaxResult addSc(@PathVariable Long articleId) {
        DqSc dqSc = new DqSc();
        dqSc.setScArticle(articleId);
        dqSc.setScUser(StpUtil.getLoginIdAsLong());
        dqSc.setCreateTime(DateUtils.getNowDate());
        dqSc.setUpdateTime(DateUtils.getNowDate());
        int i = iScService.insertDqSc(dqSc);
        return AjaxResult.success("添加成功", i);
    }

    @RequestMapping("/status/{articleId}")
    @ApiOperation(value = "查询是否收藏")
    public AjaxResult status(@PathVariable Long articleId) {
        DqSc dqSc = new DqSc();
        dqSc.setScArticle(articleId);
        dqSc.setScUser(StpUtil.getLoginIdAsLong());
        List<DqSc> dqScs = iScService.selectDqSc1(1, 5, dqSc);
        if (dqScs.size() > 0) {
            return AjaxResult.success("已收藏", 1);
        } else {
            return AjaxResult.error("未收藏", 0);
        }
    }

    @RequestMapping("/getscs/{articleId}")
    @ApiOperation(value = "查询是收藏数")
    public AjaxResult getscs(@PathVariable Long articleId) {
        int i =iScService.selectDqScCount(articleId);
        return AjaxResult.success("收藏数", i);
    }


}
